package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.entity.User;
import com.qf.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/*
杨威
2021/9/24
10:36
*/
public class UserDaoImpl implements UserDao {
    QueryRunner queryRunner=new QueryRunner();
    @Override
    public User select(String username) {
        try {
            String sql="select * from user where username=?";
            User user = queryRunner.query(DruidUtil.getConnection(), sql, new BeanHandler<>(User.class), username);
            return user;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int update(String username, String newpassword) {
        try {
            String sql="update user set password=? where username=?";
            return queryRunner.update(DruidUtil.getConnection(), sql, newpassword, username);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }

    }
}
